#include <iostream>

using namespace std;

int calc(int x)
{
	int cnt = 0;
	while(x)
	{
		cnt++;
		x &= x - 1;
	}
	return cnt;
}

int main()
{
	int T; cin >> T;
	while(T--)
	{
		int n, x; cin >> n >> x;
		if(n == 1 && x == 0) cout << -1 << endl;
		else
		{
			int cnt = calc(x);
			if(cnt == 0)
			{
				if(n % 2 == 0) cout << n << endl;
				else cout << n - 3 + 6 << endl;
			} 
			else
			{
				//			cout << cnt << endl;
			int l = n - cnt, r = n - 1;
			if(l == r && l % 2 != 0)
			{
				int ret = n - 2;
				int i = 0;
				while((x & (1 << i)) != 0) i++;
				int a = 1;
			
				while(i--) a *= 2;
//				cout << a << endl;
				ret += a + x + a;
				cout << ret << endl;
			}
			else
			{
				
				if(l % 2 != 0) l++;
				cout << x + l << endl;
			}
			}

			
		}

	}
	return 0;
}
